package com.draw.cli.gui.panel.draw;

import java.net.MalformedURLException;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.xml.ws.WebServiceException;

import com.draw.cli.gui.Loading;
import com.draw.cli.webclient.WordClient;
import com.draw.core.Word;
import com.draw.webinterface.WordInterface;

public class ChooseWordPanel extends JPanel {
	private static final long serialVersionUID = 2284055009230307597L;

	private Word[] words;

	private JRadioButton[] radioWords;
	private ButtonGroup group;

	public ChooseWordPanel() {
		this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

		words = new Word[3];

		group = new ButtonGroup();
		radioWords = new JRadioButton[this.words.length];

		for (int i = 0; this.words.length > i; i++) {
			this.radioWords[i] = new JRadioButton("(" + (i + 1) + ")");
			this.radioWords[i].setSelected(i == 0);
			group.add(this.radioWords[i]);
			this.add(this.radioWords[i]);
		}
	}

	public void initWords() throws MalformedURLException, WebServiceException,
			NullPointerException {
		// TODO Attendre l'implémentation de l'interface
		Loading.start();
		WordInterface wi = WordClient.getInstance();
		for (int i = 0; this.words.length > i; i++) {
			words[i] = wi.getRandomByLevel(i+1);
			this.radioWords[i]
					.setText("(" + (i + 1) + ")" + words[i].getWord());
			this.radioWords[i].setSelected(i == 0);
		}
		Loading.stop();
	}

	public Word getSelectedWord() {
		for (int i = 0; this.words.length > i; i++) {
			if (this.radioWords[i].isSelected()) {
				return this.words[i];
			}
		}
		return null;
	}
}
